www.gusucode.com > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序 > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序\code\PhManage\PhysicSetDlg.cpp

    //Download by http://www.NewXing.com
// PhysicSetDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PhysicM.h"
#include "PhysicSetDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPhysicSetDlg dialog


CPhysicSetDlg::CPhysicSetDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPhysicSetDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPhysicSetDlg)
	m_demo = _T("");
	m_pjc = _T("");
	m_pn = _T("");
	m_price = 0.0;
	//}}AFX_DATA_INIT
}


void CPhysicSetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPhysicSetDlg)
	DDX_Control(pDX, IDC_PROVIDER_COMBO, m_ProviderComb);
	DDX_Control(pDX, IDC_PHYSIC_LIST, m_PhysicInfoList);
	DDX_Text(pDX, IDC_DEMO_EDIT, m_demo);
	DDX_Text(pDX, IDC_PHYSIC_JC, m_pjc);
	DDX_Text(pDX, IDC_PHYSIC_NAME, m_pn);
	DDX_Text(pDX, IDC_PHYSIC_PRICE, m_price);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPhysicSetDlg, CDialog)
	//{{AFX_MSG_MAP(CPhysicSetDlg)
	ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
	ON_BN_CLICKED(IDC_DELETE_BUTTON,OnDelBtn)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPhysicSetDlg message handlers

void CPhysicSetDlg::OnAddButton() 
{
	UpdateData();
	m_pn.TrimLeft();
	m_pn.TrimRight();
	if(m_pn.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("药品名称不能为空,请输入");
		return;
	}

	CString provider;
	m_ProviderComb.GetWindowText(provider);
	if(provider.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("供应商不能为空,请选择");
		return;
	}
	if(m_price <0.00 || m_price >10000.00) 
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("单价太大或太小,请重输");
		return;
	}
	AddInfo(m_pn,m_pjc,m_price,provider,m_demo);
}

int CPhysicSetDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here

	return 0;
}

BOOL CPhysicSetDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_PhysicInfoList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	char *head[]={"序 号","药品名称","药品简称","单价","供应商","药品ID","备注"};
	int i;
	for (i=0;i<7;i++)
		m_PhysicInfoList.InsertColumn(i,head[i],(i==3)? LVCFMT_RIGHT : LVCFMT_LEFT,(i==1) ? 150 : 100,i);

	ImageList=new CImageList();
	ImageList->Create(16,16,TRUE | ILC_COLOR32,2,0);
	ImageList->Add(AfxGetApp()->LoadIcon(IDI_ICONUSER));
	ImageList->Add(AfxGetApp()->LoadIcon(IDR_MENUVIEW_TMPL));
	m_PhysicInfoList.SetImageList(ImageList,LVSIL_SMALL);// LVSIL_NORMAL);
	Init();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPhysicSetDlg::Init()
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	try{
		DB.Open("SELECT provider_id,provider_name provider_name from provider order by provider_id");
		int id=0;
		char pname[40],tmp[50];
		memset(pname,0,40);
		while(!DB.isEof())
		{
			id=DB.GetValue("provider_id");
			DB.GetValue("provider_name",pname);
			sprintf(tmp,"[%03d]%s",id,pname);
			m_ProviderComb.AddString((LPCTSTR)tmp);
			DB.Next();
		}
		DB.Open("SELECT a.physic_id pid,a.physic_name pn,a.physic_jc pj,price,b.provider_name prn,a.demo dm FROM Physic a,provider b where a.provider_id=b.provider_id order by a.physic_id");
		int no=0;
		double price=0.00;
		while(!DB.isEof())
		{
			sprintf(tmp,"%03d",no+1);
			m_PhysicInfoList.InsertItem(no,tmp,0);
			DB.GetValue("pn",pname);
			m_PhysicInfoList.SetItemText(no,1,(LPCTSTR)pname);
			DB.GetValue("pj",pname);
			m_PhysicInfoList.SetItemText(no,2,(LPCTSTR)pname);
			DB.GetValue("price",&price);
			sprintf(tmp,"%.2f",price);
			m_PhysicInfoList.SetItemText(no,3,(LPCTSTR)tmp);
			DB.GetValue("prn",pname);
			m_PhysicInfoList.SetItemText(no,4,(LPCTSTR)pname);
			id=DB.GetValue("pid");
			sprintf(pname,"%03d",id);
			m_PhysicInfoList.SetItemText(no,5,(LPCTSTR)pname);
			memset(pname,0,40);
			DB.GetValue("dm",pname);
			m_PhysicInfoList.SetItemText(no,6,(LPCTSTR)pname);
			no++;
			DB.Next();
		}
	}catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch (...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}

}
void CPhysicSetDlg::AddInfo(CString m_pname,CString m_pj,double price,CString m_prid,CString m_dm)
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	int max_id=0;
	CString provider_id=m_prid.Mid(1,3);
	try{
		DB.Open("select IDENT_CURRENT('physic') did "
			" from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='physic' ");
		double id=0;
		DB.GetValue("did",&id);
		max_id=(int)id+1;
		DB.ExecSQL("INSERT INTO physic (physic_name,physic_jc,provider_id,price,demo) "
			"VALUES('%s','%s',%s,%.2f,'%s')",m_pname,m_pj,provider_id,price,m_dm);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddLog("新增药品:%s",m_pname);
		char no[4],tmp[20];
		int icount=m_PhysicInfoList.GetItemCount();
		memset(no,0,4);
		sprintf(no,"%03d",icount+1);
		m_PhysicInfoList.InsertItem(m_PhysicInfoList.GetItemCount(),no,0);
		m_PhysicInfoList.SetItemText(icount,1,(LPCTSTR)m_pname);
		m_PhysicInfoList.SetItemText(icount,2,(LPCTSTR)m_pj);
		sprintf(tmp,"%.2f",price);
		m_PhysicInfoList.SetItemText(icount,3,(LPCTSTR)tmp);
		m_PhysicInfoList.SetItemText(icount,4,(LPCTSTR)m_prid);
		sprintf(no,"%03d",max_id);
		m_PhysicInfoList.SetItemText(icount,5,(LPCTSTR)no);
		m_PhysicInfoList.SetItemText(icount,6,(LPCTSTR)m_dm);

	}
	catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch(...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}
}
BOOL CPhysicSetDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	if(ImageList)
		delete ImageList;
	return CDialog::DestroyWindow();
}

void CPhysicSetDlg::OnOK() 
{
	// TODO: Add extra validation here
	
//	CDialog::OnOK();
}
void CPhysicSetDlg::OnDelBtn()
{
	((CPhysicMApp *)AfxGetApp())->Delete("Physic","Physic_id",&m_PhysicInfoList,5);
}